<?php
header("Content-type: text/html; charset=utf-8"); 
include "DB.class.php";

	// 显示curl 抓取结果
	// echo $is_bad_word;
	
	function get_is_bad_word($str)
	{
		$url = 'http://cn.bing.com/search?q='.urlencode($str);
		$url="https://s.taobao.com/search?q=".urlencode($str);
		$rs  = curl_post($url);
		return $rs;
	}

	function curl_get($f_url){
		//初始化
		$ch = curl_init();
		//设置选项，包括URL
		curl_setopt($ch, CURLOPT_URL, $f_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//执行并获取HTML文档内容
		$output = curl_exec($ch);
		//释放curl句柄
		curl_close($ch);

		//返回获得的数据
		return $output;
	}

	function curl_post($url, $data='', $method='GET'){   
    $curl = curl_init(); // 启动一个CURL会话  
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
    // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer  
	    if($method=='POST'){  
	        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
	        if ($data != ''){  
	            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包  
	        }  
	    }  
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
    $tmpInfo = curl_exec($curl); // 执行操作  
    curl_close($curl); // 关闭CURL会话  
    return $tmpInfo; // 返回数据   
} 
function preg_A($str){
	$char = "。、！？：；﹑•＂…‘’“”〝〞∕¦‖—　〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔！¡？¿﹖﹌﹏﹋＇´ˊˋ―﹫︳︴¯＿￣﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼（），≥";
 
		$pattern = array(
		    "/[[:punct:]]/i", //英文标点符号
		    '/['.$char.']/u', //中文标点符号
		    "/\r/",
		    "/\n/",
		    "/\t/",
		    "/ /",
		    '/[ ]{2,}/'
		);
	$str = preg_replace($pattern, '', $str);
	return $str;
}
function trimall($str){
    $qian=array(" ","　","\t","\n","\r");
    return str_replace($qian, '', $str);   
}

function mbStrSplit ($string, $len=1) {
  $start = 0;
  $strlen = mb_strlen($string);
  while ($strlen) {
  	$field=mb_substr($string,$start,$len,"utf8");

  	if ($field!="\n" && $field!="\r\n" && $field!="\t" ) {
  		$array[] = trim($field);
  		# code...
  	}

    $string = mb_substr($string, $len, $strlen,"utf8");
    $strlen = mb_strlen($string);
  }

  return $array;
}
//白名单数据变成一维数组
function white_arr($white_arr){
	foreach ($white_arr as $k => $v) {
		$arr[]=$v['title'];
		# code...
	}
	return $arr;
}


// 返回切割后的字符串数组
function split_Num($content,$num=2){
	$split_Arr= mbStrSplit(preg_A(strip_tags($content)),$num);
	return $split_Arr;
}
//白名单一维数组
$white_arr=$DB->queryAll("select * from white_list");
$white_arr=white_arr($white_arr);
//检查敏感词并入库

	while (1 <= 10) {
	$sql="select * from article where status = 0";
	$re=$DB->queryOne($sql);
	if ($re) {
		# code...
	
	$split_Arr1=split_Num($re['content'],2);
	$split_Arr2=split_Num($re['content'],3);
	$split_Arr=array_merge($split_Arr1,$split_Arr2);
	$str="";
	foreach ($split_Arr as $k => $v) {
    $is_bad_word = get_is_bad_word($v);
    sleep(1);
    $len=strlen($is_bad_word);

       	if ($len<115000) {
       			// check is white
	       		// if not - insert illegal
	       		// break
	       	if (!in_array($v,$white_arr)) {
       		$sql="insert into illegal (title) value ('".$v."')";
       		$DB->exec($sql);
       		echo 'in_error';
            }
	       	if ($str=="") {
	       		$str.=$v;
	       		# code...
	       	}else{
	       		$str.=','.$v;
	       	}
	       	echo 1;
	    }else{
	    	echo 0;
	    }
	} 
 
		    echo 'end';
		if ($str!='') {

		$sql2="update article set error='".$str."' , status = 2 where id =".$re['id'];
		}else{
		$sql2="update article set status =  1 where id =".$re['id'];	
		}
		$re=$DB->exec($sql2);
       }
		# code...
	}











